package com.mjf.jst.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.jst.domain.BcJstSalesOutDetailT;
import com.mjf.jst.domain.BcJstSalesOutT;
import com.mjf.jst.mapper.BcJstSalesOutTMapper;
import com.mjf.jst.service.BcJstSalesOutDetailTService;
import com.mjf.jst.service.BcJstSalesOutTService;
import com.mjf.jst.service.JSTAsyncService;
import com.mjf.jst.util.DateAndTimeUtils;
import com.mjf.jst.vo.JstSalesOutVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/7/12 22:37
 */
@Slf4j
@Service
public class BcJstSalesOutTServiceImpl extends ServiceImpl<BcJstSalesOutTMapper, BcJstSalesOutT> implements BcJstSalesOutTService {

    @Autowired
    private JSTAsyncService jstAsyncService;
    @Autowired
    private BcJstSalesOutDetailTService bcJstSalesOutDetailTService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncSalesout(String start, String end) {

        List<JstSalesOutVO.OutInfoDataList> vos = jstAsyncService.asyncSalesout(start, end);
        List<BcJstSalesOutT> list = new ArrayList<>();
        List<BcJstSalesOutDetailT> detailTS = new ArrayList<>();
        // 已出库
        /**
         * 状态
         * ;WaitConfirm:待出库,Confirmed:已出库,Delete:作废（订单发货中取消订单）,OuterConfirming:外部发货中;Cancelled=取消（订单发货后撤销已发货）
         */
        vos = vos.stream().filter(vo -> "Confirmed".equals(vo.getStatus())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(vos)) {
            for (JstSalesOutVO.OutInfoDataList vo : vos) {
                BcJstSalesOutT bcJstSalesOutT = new BcJstSalesOutT();
                bcJstSalesOutT.setShopId(vo.getShop_id());
                bcJstSalesOutT.setShopName(vo.getShop_name());
                bcJstSalesOutT.setIoId(vo.getIo_id());
                bcJstSalesOutT.setSoId(vo.getSo_id());
                bcJstSalesOutT.setOId(vo.getO_id());
                bcJstSalesOutT.setCreated(DateAndTimeUtils.getDateTimeByStr(vo.getCreated(), "yyyy-MM-dd HH:mm:ss"));
                bcJstSalesOutT.setRemark(vo.getRemark());
                bcJstSalesOutT.setPaidAmount(vo.getPaid_amount());
                bcJstSalesOutT.setFreeAmount(vo.getFree_amount());
                bcJstSalesOutT.setBuyerPaidAmount(vo.getBuyer_paid_amount());
                bcJstSalesOutT.setFreight(vo.getFreight());
                bcJstSalesOutT.setNode(vo.getNode());
                bcJstSalesOutT.setPushFlag(vo.getPush_flag());
                bcJstSalesOutT.setTimestamp(DateAndTimeUtils.getDateTimeByStr(vo.getTimestamp(), "yyyy-MM-dd HH:mm:ss"));
                bcJstSalesOutT.setStatus(vo.getStatus());
                bcJstSalesOutT.setModified(DateAndTimeUtils.getDateTimeByStr(vo.getModified(), "yyyy-MM-dd HH:mm:ss"));
                bcJstSalesOutT.setIoDate(DateAndTimeUtils.getDateTimeByStr(vo.getIo_date(), "yyyy-MM-dd HH:mm:ss"));
                bcJstSalesOutT.setTs(vo.getTs());
                list.add(bcJstSalesOutT);
                if (CollectionUtil.isNotEmpty(vo.getItems())) {
                    for (JstSalesOutVO.OutInfoDataItemsList item : vo.getItems()) {
                        BcJstSalesOutDetailT bcJstSalesOutDetailT = new BcJstSalesOutDetailT();
                        bcJstSalesOutDetailT.setIoiId(item.getIoi_id());
                        bcJstSalesOutDetailT.setIoId(vo.getIo_id());
                        bcJstSalesOutDetailT.setOId(vo.getO_id());
                        bcJstSalesOutDetailT.setOiId(item.getOi_id());
                        bcJstSalesOutDetailT.setSkuId(item.getSku_id());
                        bcJstSalesOutDetailT.setName(item.getName());
                        bcJstSalesOutDetailT.setQty(item.getQty());
                        bcJstSalesOutDetailT.setSalePrice(item.getSale_price());
                        bcJstSalesOutDetailT.setSaleAmount(item.getSale_amount());
                        bcJstSalesOutDetailT.setIsGift(BooleanUtil.isTrue(item.getIs_gift()) ? 1 : 0);
                        bcJstSalesOutDetailT.setCombineSkuId(item.getCombine_sku_id());
                        bcJstSalesOutDetailT.setOuterOiId(item.getOuter_oi_id());
                        bcJstSalesOutDetailT.setRawSoId(item.getRaw_so_id());
                        bcJstSalesOutDetailT.setBuyerPaidAmount(item.getBuyer_paid_amount());
//                        bcJstSalesOutDetailT.setTimestamp(DateAndTimeUtils.getDateTimeByStr(item.getTimestamp(), "yyyy-MM-dd HH:mm:ss"));
                        detailTS.add(bcJstSalesOutDetailT);
                    }
                }
            }
        }

        if (CollectionUtil.isNotEmpty(list)) {
            log.info("asyncSalesout 批量保存数据 {}", list.size());
            this.saveOrUpdateBatch(list);
        }
        if (CollectionUtil.isNotEmpty(detailTS)) {
            log.info("asyncSalesout 批量保存数据 {}", detailTS.size());
            bcJstSalesOutDetailTService.saveOrUpdateBatch(detailTS);
        }

    }
}
